package cn.kgc.controller;

import cn.kgc.common.CrowdFundingConstant;
import cn.kgc.common.ResultEntity;
import cn.kgc.pojo.Building;
import cn.kgc.service.BuildingService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-03-16 9:22
 */
@Controller
public class BuildingController {

    @Resource
    BuildingService buildingService;

    @RequestMapping("/building/pageinfo/name")
//    @ResponseBody
    public String  buildingpageinfoname(  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                          @RequestParam(value = "keyword", defaultValue = "") String keyword,
                                          Model model){
        PageInfo<Building> pageInfo = buildingService.queryForKeywordSearchBuilding(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return  "body/building/building-member-list1";
    }

    //增加
    @RequestMapping("/insert/building")
    public String insertbuilding(Building building){
        buildingService.insertBuilding(building);
        return "redirect:/building/pageinfo/name";
    }

    //修改回显
    @RequestMapping("/select/ById/building")
    public String selectbuilding(Integer id,Model model){
       Building b=  buildingService.selectByIdBuilding(id);
        model.addAttribute("b",b);

        return "body/building/building-update";
    }
    //修改
    @RequestMapping("/update/building")
    public String updatebuilding(Building building,Integer pageNum){
        buildingService.updateBuilding(building);
        return "redirect:/building/pageinfo/name?pageNum="+pageNum;
    }
    //批量删除
    @RequestMapping("delete/building")
    @ResponseBody
    public ResultEntity<String> deletebuilding(@RequestBody List<Integer> id){
        buildingService.deleteBuilding(id);
        return ResultEntity.successWithoutData();
    }
    //单条删除
    @RequestMapping("delete/building/id")
    public String deleteBuildingssss(@RequestParam("id") Integer id) {
        buildingService.removedeleteBuilding(id);
        return "redirect:/building/pageinfo/name";
    }

    @RequestMapping("/select/xiang/building")
    public String selectxiangbuilding(Integer id,Model model){
        Building b=  buildingService.selectByIdBuilding(id);
        model.addAttribute("b",b);
        return "body/building/building-xiang";
    }
}
